package com.tc.gateway.tunnel;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

public class MultiCast {
	InetAddress group = null;
	MulticastSocket socket = null;
	InputStream inputStream = null;
	OutputStream outputStream = null;
	int port;
	boolean listen;
	public MultiCast(String addr, int p, boolean join) throws IOException{
		this.port = p;
		this.listen = join;

            group = InetAddress.getByName(addr);

      
           socket = new MulticastSocket(port);

    	   if(join)
    		   socket.joinGroup(group);
     
	}
	
	public InputStream getInputStream() {
		if(!listen)
			throw new RuntimeException("this socket has not join the group");
		if(inputStream == null)
			inputStream = new MultiInputStream(group, socket,port);
		return inputStream;
	}
	public OutputStream getOutputStream() {
		if(outputStream == null)
			  outputStream = new MultiOutputStream(group, socket, port);
		return outputStream;
	}
	
}
